package com.huilian.iotbox.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huilian.iotbox.admin.dao.BrandInfoDao;
import com.huilian.iotbox.admin.dao.MerchantStoreDao;
import com.huilian.iotbox.admin.service.MerchantStoreService;
import com.huilian.iotbox.admin.service.SystemRiskConfigService;
import com.huilian.iotbox.data.common.RedisKey;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.MerchantStoreDto;
import com.huilian.iotbox.data.dto.MerchantStoreSkuDto;
import com.huilian.iotbox.data.enums.*;
import com.huilian.iotbox.data.po.BrandInfo;
import com.huilian.iotbox.data.po.MerchantStore;
import com.huilian.iotbox.data.po.SystemRiskConfig;
import com.huilian.iotbox.data.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author 谢洛涛
 * @date 2021/3/31 11:04
 */
@Service
@Slf4j
public class MerchantStoreServiceImpl implements MerchantStoreService {
    @Resource
    private MerchantStoreDao merchantStoreDao;
    @Resource
    private SystemRiskConfigService systemConfigService;
    @Resource
    private RedisTemplate<String, Serializable> redisCacheTemplate;

    @Resource
    private BrandInfoDao brandInfoDao;

    @Override
    public ServerResponse findAllMerchantStoreByUserInfo(MerchantStoreDto merchantStoreDto) {
        return ServerResponse.createBySuccess(merchantStoreDao.findAllMerchantStoreByMchId(merchantStoreDto));
    }

    @Override
    public PageInfo findMerchantStoreList(MerchantStoreDto merchantStoreDto) {
        PageHelper.startPage(merchantStoreDto.getPageNum(), merchantStoreDto.getPageSize());
        List<MerchantStoreVo> merchantStoreVoList = merchantStoreDao.findMerchantStoreList(merchantStoreDto);
        List<SystemRiskConfig> systemRiskConfigList = systemConfigService.findAll();
        Map<Integer, SystemRiskConfig> systemRiskConfigMap = systemRiskConfigList.stream()
                .collect(Collectors.toMap(SystemRiskConfig::getType, Function.identity()));
        for (MerchantStoreVo merchantStoreVo : merchantStoreVoList) {
            if (merchantStoreVo.getMemberMinuteQuota() == null) {
                merchantStoreVo.setMemberMinuteQuota(systemRiskConfigMap.get(SystemRiskConfigTypeEnum.MINUTE.getCode()).getMoney());
            }
            if (merchantStoreVo.getMemberMinutePayCount() == null) {
                merchantStoreVo.setMemberMinutePayCount(systemRiskConfigMap.get(SystemRiskConfigTypeEnum.MINUTE.getCode()).getCount());
            }
            if (merchantStoreVo.getMemberHourQuota() == null) {
                merchantStoreVo.setMemberHourQuota(systemRiskConfigMap.get(SystemRiskConfigTypeEnum.HOUR.getCode()).getMoney());
            }
            if (merchantStoreVo.getMemberHourPayCount() == null) {
                merchantStoreVo.setMemberHourPayCount(systemRiskConfigMap.get(SystemRiskConfigTypeEnum.HOUR.getCode()).getCount());
            }
            if (merchantStoreVo.getMemberDayQuota() == null) {
                merchantStoreVo.setMemberDayQuota(systemRiskConfigMap.get(SystemRiskConfigTypeEnum.DAY.getCode()).getMoney());
            }
            if (merchantStoreVo.getMemberDayPayCount() == null) {
                merchantStoreVo.setMemberDayPayCount(systemRiskConfigMap.get(SystemRiskConfigTypeEnum.DAY.getCode()).getCount());
            }
        }
        PageInfo pagResult = new PageInfo(merchantStoreVoList);
        return pagResult;
    }

    @Override
    public Integer findDayStoreNewNumByAgencyId(Integer agencyId, String startTime, String endTime) {
        return merchantStoreDao.findDayStoreNewNumByAgencyId(agencyId, startTime, endTime);
    }

    @Override
    public ServerResponse updateMerchantStore(MerchantStoreDto merchantStoreDto) {
        if (!StringUtils.isEmpty(merchantStoreDto.getDayQuota()) && merchantStoreDto.getDayQuota().compareTo(new BigDecimal(1000)) < 0) {
            return ServerResponse.createByErrorMessage("场地每天收款限额度最低1000");
        }
        if (!StringUtils.isEmpty(merchantStoreDto.getMemberMinuteQuota()) && merchantStoreDto.getMemberMinuteQuota().compareTo(new BigDecimal(100)) < 0) {
            return ServerResponse.createByErrorMessage("会员每分钟支付额度最低100");
        }
        if (!StringUtils.isEmpty(merchantStoreDto.getMemberMinutePayCount()) && merchantStoreDto.getMemberMinutePayCount() < 1) {
            return ServerResponse.createByErrorMessage("会员每分钟支付额笔数最低1");
        }
        if (!StringUtils.isEmpty(merchantStoreDto.getMemberHourQuota()) && merchantStoreDto.getMemberHourQuota().compareTo(new BigDecimal(300)) < 0) {
            return ServerResponse.createByErrorMessage("会员每小时支付额度最低300");
        }
        if (!StringUtils.isEmpty(merchantStoreDto.getMemberHourPayCount()) && merchantStoreDto.getMemberHourPayCount() < 1) {
            return ServerResponse.createByErrorMessage("会员每小时支付额笔数最低1");
        }
        if (!StringUtils.isEmpty(merchantStoreDto.getMemberDayQuota()) && merchantStoreDto.getMemberDayQuota().compareTo(new BigDecimal(1000)) < 0) {
            return ServerResponse.createByErrorMessage("会员每天支付额度最低1000");
        }
        if (!StringUtils.isEmpty(merchantStoreDto.getMemberDayPayCount()) && merchantStoreDto.getMemberDayPayCount() < 1) {
            return ServerResponse.createByErrorMessage("会员每天支付额笔数最低1");
        }
        Integer count = merchantStoreDao.updateLimit(merchantStoreDto);
        if (count > 0) {
            String storeKey = String.format(RedisKey.MERCHANT_STORE, merchantStoreDto.getId());
            redisCacheTemplate.delete(storeKey);
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByErrorMessage("修改失败");
    }

    @Override
    public Integer findSetStoreNoTrade(Integer agencyId, Set<Integer> set) {
        return merchantStoreDao.findSetStoreNoTrade(agencyId, set);
    }

    @Override
    public MerchantStore findMerchantStoreById(Integer id) {
        return merchantStoreDao.findMerchantStoreById(id);
    }

    @Override
    public List<MerchantStoreVo> findAllMerchantStoreByMerchantId(String merchantId) {
        return merchantStoreDao.findAllMerchantStoreByMerchantId(merchantId);
    }

    @Override
    public MerchantStore findMerchantStoreByDeviceNo(String deviceNo) {
        return merchantStoreDao.findMerchantStoreByDeviceNo(deviceNo);
    }


    @Override
    public PageInfo<MerchantStoreVo> findMerchantStoreListByUserId(MerchantStoreDto merchantStoreDto) {
        if (RoleEnum.SUB_ACCOUNT.getRoleId().equals(merchantStoreDto.getUserInfo().getRoleId()) && merchantStoreDto.getUserInfo().getStoreIdList().size() == 0) {
            return new PageInfo<>(new ArrayList<>());
        }
        PageHelper.startPage(merchantStoreDto.getPageNum(), merchantStoreDto.getPageSize());
        return new PageInfo<>(merchantStoreDao.findMerchantStoreListByUserId(merchantStoreDto));
    }

    @Override
    public ServerResponse updateIsExchange(MerchantStoreDto merchantStoreDto) {
        Integer count = merchantStoreDao.updateIsExchange(merchantStoreDto);
        if (count > 0) {
            return ServerResponse.createBySuccess("操作成功");
        } else {
            return ServerResponse.createByErrorMessage("操作失败");
        }
    }

    @Override
    @Transactional
    public ServerResponse batchUpdateIsExchange(MerchantStoreDto merchantStoreDto) {
        // 查询当前商户能批量修改的场地
        List<MerchantStoreVo> merchantStoreList = merchantStoreDao.findMerchantStoreListByUserId(merchantStoreDto);
        Integer count = 0;
        for (MerchantStoreVo merchantStoreVo : merchantStoreList) {
            merchantStoreDto.setId(merchantStoreVo.getId());
            count = merchantStoreDao.updateIsExchange(merchantStoreDto);
        }
        if (count > 0) {
            return ServerResponse.createBySuccess("操作成功");
        } else {
            return ServerResponse.createByErrorMessage("操作失败");
        }
    }

    @Override
    public MerchantStore findOneMerchantStoreByCondition(MerchantStoreDto merchantStoreDto) {
        return merchantStoreDao.findOneMerchantStoreByCondition(merchantStoreDto);
    }

    @Override
    public MerchantStoreVo findMerchantStoreByUserIdByStoreId(MerchantStore merchantStore) {
        MerchantStoreVo merchantStoreVo = merchantStoreDao.findMerchantStoreByUserIdByStoreId(merchantStore);
        if (StringUtils.isEmpty(merchantStoreVo.getServiceMobilePhone())) {
            // 品牌信息
            BrandInfo brandInfoVo = brandInfoDao.findByUserId(merchantStore.getUserId());
            if (!StringUtils.isEmpty(brandInfoVo)) {
                merchantStoreVo.setServiceMobilePhone(brandInfoVo.getServiceMobilePhone());
            }
        }
        return merchantStoreVo;
    }
}
